package com.bage.finance.admin.api.controller;

import com.bage.common.dto.ApiResponse;
import com.bage.finance.biz.dto.form.*;
import com.bage.finance.biz.dto.vo.ListFileVo;
import com.bage.finance.biz.service.FileService;
import com.bage.mybatis.help.PageInfo;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Api(tags = "文件管理")
@RestController
@RequestMapping(value = "/file")
@RequiredArgsConstructor
public class FileController {

    final FileService fileService;

    @ApiOperation(value = "合并上传文件")
    @PostMapping(value = "/composeFile")
    public ApiResponse<String> composeFile(@Valid @RequestBody ComposeFileForm form) {
        return ApiResponse.success(fileService.composeFile(form));
    }

    @ApiOperation(value = "分片上传文件(大于5M分片,小于等于5M不分片)")
    @PostMapping(value = "/uploadPart")
    public ApiResponse<String> uploadPart(@Valid UploadFileForm form) throws Exception {
        return ApiResponse.success(fileService.uploadPart(form));
    }

    @ApiOperation(value = "查询文件列表")
    @PostMapping(value = "/listByIds")
    public ApiResponse<List<ListFileVo>> listByIds(@Valid @RequestBody ListFileByIdsForm form) throws Exception {
        return ApiResponse.success(fileService.listByIds(form));
    }

    @ApiOperation(value = "删除图片")
    @PostMapping(value = "/del")
    public ApiResponse<Boolean> del(@Valid @RequestBody DelForm form) {
        return ApiResponse.success(fileService.del(form));
    }

    @ApiOperation(value = "获取图片地址")
    @GetMapping("/getPicUrl")
    public ApiResponse<String> getPicUrl(@Valid @NotNull Long id) throws Exception {
        return ApiResponse.success(fileService.getPicUrl(id));
    }

    @ApiOperation(value = "查询文件列表")
    @PostMapping("/list")
    public ApiResponse<PageInfo<ListFileVo>> listByIds(@Valid @RequestBody ListFileForm form) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ApiResponse.success(fileService.list(form));
    }
}
